<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>游戏抽奖系统</title>
    <style>
        :root {
            --primary-color: #d4af37; /* 金色 */
            --primary-hover: #c9a227;
            --secondary-color: #b8860b; /* 暗金色 */
            --dark-color: #1a1a1a;
            --light-color: #f8f8f8;
            --border-color: rgba(212, 175, 55, 0.3);
            --panel-bg: rgba(30, 30, 30, 0.9);
            --input-bg: rgba(50, 50, 50, 0.8);
            --danger-color: #e74c3c;
            --danger-hover: #c0392b;
        }
        
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start;
            min-height: 100vh;
            background: linear-gradient(135deg, #2c3e50 0%, #1a1a1a 100%);
            color: var(--light-color);
            padding: 20px;
            position: relative;
            overflow-x: hidden;
        }
        
        .container {
            text-align: center;
            max-width: 900px;
            width: 100%;
            background-color: var(--panel-bg);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
            padding: 30px;
            margin-bottom: 20px;
            border: 1px solid var(--border-color);
        }
        
        /* 大标题样式 */
        .main-title {
            color: var(--primary-color);
            margin-bottom: 30px;
            font-size: 2.8rem;
            text-shadow: 0 2px 10px rgba(212, 175, 55, 0.5);
            position: relative;
            display: inline-block;
            padding: 0 20px;
        }
        
        .main-title::before, .main-title::after {
            content: "✨";
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            font-size: 1.5rem;
        }
        
        .main-title::before {
            left: -10px;
        }
        
        .main-title::after {
            right: -10px;
        }
        
        /* 抽奖结果卡片 */
        .result-card {
            background: rgba(40, 40, 40, 0.8);
            border-radius: 15px;
            padding: 25px;
            text-align: center;
            box-shadow: 0 10px 20px rgba(0,0,0,0.3);
            border: 1px solid var(--border-color);
            max-width: 400px;
            margin: 20px auto;
            position: relative;
            overflow: hidden;
        }
        
        .result-card::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
        }
        
        .result-header {
            font-size: 1.5rem;
            color: var(--primary-color);
            margin-bottom: 20px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.5);
        }
        
        /* 游戏卡片样式 - 添加中奖特效 */
        .game-card {
            background: linear-gradient(135deg, #3a3a3a 0%, #2a2a2a 100%);
            padding: 25px;
            border-radius: 10px;
            border-left: 5px solid var(--primary-color);
            animation: fadeIn 0.5s;
            margin: 15px 0;
            position: relative;
            transition: all 0.3s;
        }
        
        .game-card.winner {
            animation: winnerPulse 0.5s ease-in-out 3;
            box-shadow: 0 0 20px rgba(212, 175, 55, 0.5);
            transform: scale(1.02);
        }
        
        .game-card::after {
            content: "";
            position: absolute;
            bottom: 0;
            right: 0;
            width: 50px;
            height: 50px;
            background: radial-gradient(circle, rgba(212,175,55,0.2) 0%, rgba(0,0,0,0) 70%);
        }
        
        .game-icon {
            font-size: 3rem;
            margin-bottom: 10px;
            filter: drop-shadow(0 0 5px rgba(0,0,0,0.5));
        }
        
        .game-name {
            font-size: 1.8rem;
            font-weight: bold;
            margin: 10px 0;
            color: var(--primary-color);
        }
        
        .game-percent {
            color: #aaa;
            font-size: 1rem;
        }
        
        /* 游戏列表样式 */
        .game-list {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 15px;
            margin: 30px 0;
        }
        
        .game-item {
            background: rgba(50,50,50,0.5);
            padding: 15px 20px;
            border-radius: 8px;
            min-width: 120px;
            transition: all 0.3s;
            border: 1px solid rgba(255,255,255,0.1);
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        }
        
        .game-item.highlight {
            background: linear-gradient(135deg, rgba(212,175,55,0.3) 0%, rgba(40,40,40,1) 100%);
            transform: scale(1.05);
            box-shadow: 0 0 15px rgba(212,175,55,0.5);
            border-color: var(--primary-color);
        }
        
        .game-item-name {
            color: white;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .game-item-percent {
            color: var(--primary-color);
            font-size: 0.9rem;
        }
        
        /* 按钮样式 - 全面优化 */
        .action-buttons {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
            margin: 30px 0 20px;
        }
        
        button {
            padding: 12px 20px;
            font-size: 1rem;
            color: var(--dark-color);
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            letter-spacing: 0.5px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
            background-color: var(--primary-color);
            position: relative;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            white-space: nowrap;
            min-height: 44px;
        }
        
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(212, 175, 55, 0.4);
        }
        
        button:active {
            transform: translateY(1px);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }
        
        button:disabled {
            background-color: #555 !important;
            color: #999 !important;
            cursor: not-allowed;
            transform: none !important;
            box-shadow: none !important;
            opacity: 0.7;
        }
        
        .btn-primary {
            background-color: var(--primary-color);
            background-image: linear-gradient(to right, var(--primary-color), var(--primary-hover));
        }
        
        .btn-primary:hover {
            background-image: linear-gradient(to right, var(--primary-hover), var(--primary-color));
        }
        
        .btn-secondary {
            background-color: var(--secondary-color);
            background-image: linear-gradient(to right, var(--secondary-color), #d4a017);
            color: white;
        }
        
        .btn-secondary:hover {
            background-image: linear-gradient(to right, #d4a017, var(--secondary-color));
        }
        
        .btn-danger {
            background-color: var(--danger-color);
            background-image: linear-gradient(to right, var(--danger-color), var(--danger-hover));
            color: white;
        }
        
        .btn-danger:hover {
            background-image: linear-gradient(to right, var(--danger-hover), var(--danger-color));
        }
        
        /* 开始按钮特殊样式 */
        #auto-spin-btn {
            margin: 0 auto;
            padding: 15px 30px;
            font-size: 1.2rem;
            border-radius: 50px;
            background-image: linear-gradient(135deg, var(--primary-color), #f1c40f);
            box-shadow: 0 4px 15px rgba(212, 175, 55, 0.4);
            position: relative;
            overflow: hidden;
            z-index: 1;
        }
        
        #auto-spin-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-image: linear-gradient(135deg, #f1c40f, var(--primary-color));
            opacity: 0;
            z-index: -1;
            transition: opacity 0.3s;
        }
        
        #auto-spin-btn:hover::before {
            opacity: 1;
        }
        
        #auto-spin-btn:active {
            transform: translateY(2px);
            box-shadow: 0 2px 5px rgba(212, 175, 55, 0.4);
        }
        
        /* 输入框样式 - 全面优化 */
        input, select, textarea {
            background-color: var(--input-bg);
            border: 1px solid var(--border-color);
            color: var(--light-color);
            padding: 10px 15px;
            border-radius: 8px;
            transition: all 0.3s ease;
            font-size: 1rem;
            width: 100%;
            box-sizing: border-box;
            outline: none;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
        }
        
        input:focus, select:focus, textarea:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(212, 175, 55, 0.3), inset 0 1px 3px rgba(0, 0, 0, 0.2);
        }
        
        input[type="color"] {
            height: 40px;
            width: 60px;
            padding: 3px;
            cursor: pointer;
            border-radius: 4px;
        }
        
        input[type="checkbox"] {
            width: 20px;
            height: 20px;
            cursor: pointer;
            accent-color: var(--primary-color);
        }
        
        /* 面板样式 */
        .panel {
            background-color: var(--panel-bg);
            border-radius: 15px;
            padding: 25px;
            margin-top: 30px;
            border: 1px solid var(--border-color);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            position: relative;
            overflow: hidden;
        }
        
        .panel::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
        }
        
        .panel-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 1px solid var(--border-color);
        }
        
        .panel-title {
            font-size: 1.5rem;
            font-weight: bold;
            color: var(--primary-color);
            text-shadow: 0 1px 2px rgba(0,0,0,0.5);
        }
        
        .close-btn {
            background: none;
            border: none;
            font-size: 1.8rem;
            cursor: pointer;
            color: var(--primary-color);
            transition: color 0.3s;
            line-height: 1;
            padding: 0 10px;
        }
        
        .close-btn:hover {
            color: white;
        }
        
        /* 编辑控件样式 - 优化为三行布局 */
        .edit-controls {
            display: flex;
            flex-direction: column;
            gap: 20px;
            margin-bottom: 25px;
        }
        
        .edit-control {
            display: flex;
            flex-direction: column;
            background-color: rgba(50, 50, 50, 0.8);
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            transition: all 0.3s;
            border: 1px solid var(--border-color);
            gap: 15px;
            position: relative;
        }
        
        .edit-control-row {
            display: flex;
            align-items: center;
            gap: 15px;
            width: 100%;
        }
        
        .edit-control-row label {
            font-weight: bold;
            color: var(--primary-color);
            min-width: 60px;
            flex-shrink: 0;
        }
        
        .edit-control-row input {
            flex-grow: 1;
        }
        
        .edit-control-row .input-group {
            display: flex;
            align-items: center;
            gap: 10px;
            flex-grow: 1;
        }
        
        .edit-control-row .input-group label {
            min-width: auto;
        }
        
        .edit-control:hover {
            box-shadow: 0 5px 15px rgba(212, 175, 55, 0.2);
            transform: translateY(-2px);
        }
        
        .delete-row {
            display: flex;
            justify-content: flex-end;
            margin-top: 10px;
        }
        
        /* 统计表格样式 */
        .stats-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        .stats-table th, .stats-table td {
            padding: 12px 15px;
            text-align: center;
            border: 1px solid var(--border-color);
        }
        
        .stats-table th {
            background-color: var(--primary-color);
            color: var(--dark-color);
            font-weight: bold;
        }
        
        .stats-table tr:nth-child(even) {
            background-color: rgba(50, 50, 50, 0.8);
        }
        
        .stats-table tr:hover {
            background-color: rgba(212, 175, 55, 0.1);
        }
        
        /* 历史记录样式 */
        .history-panel {
            max-height: 400px;
            overflow-y: auto;
            padding-right: 10px;
        }
        
        .history-item {
            background-color: rgba(50, 50, 50, 0.8);
            padding: 15px 20px;
            border-radius: 8px;
            margin-bottom: 15px;
            border-left: 4px solid var(--primary-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: all 0.3s;
        }
        
        .history-item:hover {
            transform: translateX(5px);
        }
        
        .history-item:nth-child(even) {
            background-color: rgba(60, 60, 60, 0.8);
        }
        
        .history-index {
            color: var(--primary-color);
            font-weight: bold;
            margin-right: 15px;
            min-width: 30px;
        }
        
        .history-name {
            flex-grow: 1;
            text-align: left;
            padding-left: 10px;
            font-weight: bold;
        }
        
        .history-time {
            color: #aaa;
            font-size: 0.9rem;
            min-width: 80px;
            text-align: right;
        }
        
        /* 系统设置样式 */
        .settings-item {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
            padding: 15px;
            background-color: rgba(50,50,50,0.5);
            border-radius: 8px;
            transition: all 0.3s;
        }
        
        .settings-item:hover {
            background-color: rgba(50,50,50,0.7);
        }
        
        .settings-item label {
            margin-left: 10px;
            cursor: pointer;
            flex-grow: 1;
        }
        
        /* 动画效果 */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
        
        @keyframes winnerPulse {
            0% { transform: scale(1); box-shadow: 0 0 10px rgba(212, 175, 55, 0.5); }
            50% { transform: scale(1.03); box-shadow: 0 0 25px rgba(212, 175, 55, 0.8); }
            100% { transform: scale(1); box-shadow: 0 0 10px rgba(212, 175, 55, 0.5); }
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            
            .main-title {
                font-size: 2rem;
                margin-bottom: 20px;
                padding: 0 15px;
            }
            
            .game-card {
                padding: 15px;
            }
            
            .game-name {
                font-size: 1.5rem;
            }
            
            .action-buttons {
                grid-template-columns: 1fr 1fr;
                gap: 10px;
            }
            
            button {
                padding: 10px;
                font-size: 0.9rem;
                min-height: 40px;
            }
            
            #auto-spin-btn {
                padding: 12px 20px;
                font-size: 1.1rem;
            }
            
            .panel {
                padding: 15px;
                margin-top: 20px;
            }
            
            .edit-control {
                padding: 15px;
                gap: 10px;
            }
            
            .edit-control-row {
                flex-direction: column;
                align-items: flex-start;
                gap: 8px;
            }
            
            .edit-control-row label {
                min-width: auto;
                font-size: 0.9rem;
            }
            
            .edit-control-row .input-group {
                width: 100%;
            }
            
            .edit-control-row input[type="color"] {
                width: 100%;
                max-width: 60px;
            }
            
            .edit-control-row input[type="number"] {
                width: 100%;
                max-width: 100px;
            }
            
            input, select, textarea {
                padding: 8px 12px;
                font-size: 0.9rem;
            }
            
            .panel-title {
                font-size: 1.3rem;
            }
            
            .stats-table th, 
            .stats-table td {
                padding: 8px 10px;
                font-size: 0.9rem;
            }
            
            .history-item {
                padding: 10px 15px;
                font-size: 0.9rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="main-title">今天玩点啥</h1>
        
        <!-- 抽奖结果展示区 -->
        <div class="result-card">
            <div class="result-header">🎮 抽奖结果 🎮</div>
            <div id="auto-result">
                <div class="game-card" style="border-left-color: #d4af37">
                    <div class="game-icon">🎮</div>
                    <div class="game-name" style="color: #d4af37">要玩这个嘛</div>
                    <div class="game-percent">点击下方按钮开始抽奖</div>
                </div>
            </div>
            
            <button id="auto-spin-btn" class="btn-primary">开始吧</button>
        </div>
        
        <!-- 游戏列表展示 -->
        <h3>🎮 可选游戏列表</h3>
        <div class="game-list" id="game-list">
            <!-- 游戏列表将在这里动态生成 -->
        </div>
        
        <!-- 操作按钮 -->
        <div class="action-buttons">
            <button id="manage-btn" class="btn-secondary">⚙️ 游戏管理</button>
            <button id="add-game-btn" class="btn-secondary">➕ 添加游戏</button>
            <button id="stats-btn" class="btn-secondary">📊 统计数据</button>
            <button id="history-btn" class="btn-secondary">📜 中奖记录</button>
            <button id="random-btn" class="btn-secondary">🎲 随机权重</button>
            <button id="settings-btn" class="btn-secondary">⚙️ 系统设置</button>
        </div>
        
        <!-- 游戏管理面板 -->
        <div id="manage-panel" class="panel" style="display: none;">
            <div class="panel-header">
                <h2 class="panel-title">⚙️ 游戏管理</h2>
                <button class="close-btn" id="close-manage-panel">&times;</button>
            </div>
            <div id="edit-controls" class="edit-controls"></div>
            <div class="action-buttons">
                <button id="save-btn" class="btn-primary">💾 保存设置</button>
                <button id="reset-btn" class="btn-secondary">🔄 重置权重</button>
            </div>
        </div>
        
        <!-- 添加游戏面板 -->
        <div id="add-game-panel" class="panel" style="display: none;">
            <div class="panel-header">
                <h2 class="panel-title">➕ 添加新游戏</h2>
                <button class="close-btn" id="close-add-game-panel">&times;</button>
            </div>
            <div class="edit-controls">
                <div class="edit-control">
                    <label for="new-game-name">游戏名称:</label>
                    <input type="text" id="new-game-name" placeholder="输入游戏名称">
                </div>
                <div class="edit-control">
                    <label for="new-game-color">颜色:</label>
                    <input type="color" id="new-game-color" value="#FF5733">
                </div>
                <div class="edit-control">
                    <label for="new-game-weight">权重值:</label>
                    <input type="number" id="new-game-weight" min="0" value="30">
                    <small style="color: #aaa;">(设为0则不参与抽奖)</small>
                </div>
            </div>
            <div class="action-buttons">
                <button id="confirm-add-btn" class="btn-primary">✅ 添加游戏</button>
            </div>
        </div>
        
        <!-- 统计数据面板 -->
        <div id="stats-panel" class="panel" style="display: none;">
            <div class="panel-header">
                <h2 class="panel-title">📊 统计数据</h2>
                <button class="close-btn" id="close-stats-panel">&times;</button>
            </div>
            <table class="stats-table">
                <thead>
                    <tr>
                        <th>游戏名称</th>
                        <th>权重值</th>
                        <th>百分比</th>
                    </tr>
                </thead>
                <tbody id="stats-body">
                    <!-- 统计数据将在这里动态生成 -->
                </tbody>
            </table>
        </div>
        
        <!-- 中奖记录面板 -->
        <div id="history-panel" class="panel" style="display: none;">
            <div class="panel-header">
                <h2 class="panel-title">📜 中奖记录</h2>
                <button class="close-btn" id="close-history-panel">&times;</button>
            </div>
            <div class="history-panel" id="history-list">
                <!-- 中奖记录将在这里动态生成 -->
            </div>
            <div class="action-buttons">
                <button id="clear-history-btn" class="btn-danger">🗑️ 清空记录</button>
            </div>
        </div>
        
        <!-- 系统设置面板 -->
        <div id="settings-panel" class="panel" style="display: none;">
            <div class="panel-header">
                <h2 class="panel-title">⚙️ 系统设置</h2>
                <button class="close-btn" id="close-settings-panel">&times;</button>
            </div>
            <div class="edit-controls">
                <div class="settings-item">
                    <input type="checkbox" id="keep-weight-setting">
                    <label for="keep-weight-setting">抽奖时不修改权重值</label>
                </div>
                <div class="settings-item">
                    <input type="checkbox" id="exclude-zero-setting" checked>
                    <label for="exclude-zero-setting">权重值为0不参与随机</label>
                </div>
                <!-- 可以在这里添加更多设置项 -->
                <div class="settings-item">
                    <input type="checkbox" id="setting2" disabled>
                    <label for="setting2">更多设置(开发中)</label>
                </div>
            </div>
            <div class="action-buttons">
                <button id="save-settings-btn" class="btn-primary">💾 保存设置</button>
            </div>
        </div>
    </div>

    <script>
        // 游戏数据
        let games = [
            { name: "王者荣耀", color: "#FF5733", weight: 30 },
            { name: "和平精英", color: "#33FF57", weight: 30 },
            { name: "金铲铲之战", color: "#3357FF", weight: 30 }
        ];
        
        // 中奖记录
        let history = [];
        
        // 系统设置
        let settings = {
            keepWeight: false,
            excludeZeroWeight: true
        };
        
        // DOM元素
        const autoResult = document.getElementById('auto-result');
        const gameList = document.getElementById('game-list');
        const autoSpinBtn = document.getElementById('auto-spin-btn');
        const historyList = document.getElementById('history-list');
        const editControls = document.getElementById('edit-controls');
        const statsBody = document.getElementById('stats-body');
        
        // 面板元素
        const managePanel = document.getElementById('manage-panel');
        const addGamePanel = document.getElementById('add-game-panel');
        const statsPanel = document.getElementById('stats-panel');
        const historyPanel = document.getElementById('history-panel');
        const settingsPanel = document.getElementById('settings-panel');
        
        // 按钮元素
        const manageBtn = document.getElementById('manage-btn');
        const addGameBtn = document.getElementById('add-game-btn');
        const statsBtn = document.getElementById('stats-btn');
        const historyBtn = document.getElementById('history-btn');
        const randomBtn = document.getElementById('random-btn');
        const settingsBtn = document.getElementById('settings-btn');
        const saveBtn = document.getElementById('save-btn');
        const resetBtn = document.getElementById('reset-btn');
        const confirmAddBtn = document.getElementById('confirm-add-btn');
        const clearHistoryBtn = document.getElementById('clear-history-btn');
        const saveSettingsBtn = document.getElementById('save-settings-btn');
        const closeManagePanel = document.getElementById('close-manage-panel');
        const closeAddGamePanel = document.getElementById('close-add-game-panel');
        const closeStatsPanel = document.getElementById('close-stats-panel');
        const closeHistoryPanel = document.getElementById('close-history-panel');
        const closeSettingsPanel = document.getElementById('close-settings-panel');
        
        // 设置元素
        const keepWeightSetting = document.getElementById('keep-weight-setting');
        const excludeZeroSetting = document.getElementById('exclude-zero-setting');
        
        // 表单元素
        const newGameName = document.getElementById('new-game-name');
        const newGameColor = document.getElementById('new-game-color');
        const newGameWeight = document.getElementById('new-game-weight');
        
        // 计算百分比
        function calculatePercentages() {
            const totalWeight = settings.excludeZeroWeight 
                ? games.reduce((sum, game) => game.weight > 0 ? sum + game.weight : sum, 0)
                : games.reduce((sum, game) => sum + game.weight, 0);
            
            games.forEach(game => {
                if (settings.excludeZeroWeight && game.weight <= 0) {
                    game.percentage = '0%';
                } else {
                    game.percentage = totalWeight > 0 ? ((game.weight / totalWeight) * 100).toFixed(1) + '%' : '0%';
                }
            });
        }
        
        // 更新游戏列表显示
        function updateGameList() {
            calculatePercentages();
            gameList.innerHTML = '';
            games.forEach((game, index) => {
                if (!settings.excludeZeroWeight || game.weight > 0) {
                    const item = document.createElement('div');
                    item.className = 'game-item';
                    item.innerHTML = `
                        <div class="game-item-name" style="color: ${game.color}">${game.name}</div>
                        <div class="game-item-percent">${game.percentage}</div>
                    `;
                    item.dataset.index = index;
                    gameList.appendChild(item);
                }
            });
        }
        
        // 更新抽奖结果显示
        function updateResult(selectedGame) {
            if (!selectedGame) {
                autoResult.innerHTML = `
                    <div class="game-card" style="border-left-color: #d4af37">
                        <div class="game-icon">🎮</div>
                        <div class="game-name" style="color: #d4af37">要玩这个嘛</div>
                        <div class="game-percent">暂无游戏数据</div>
                    </div>
                `;
                return;
            }
            
            // 移除之前的winner类
            const prevWinner = document.querySelector('.game-card.winner');
            if (prevWinner) {
                prevWinner.classList.remove('winner');
            }
            
            autoResult.innerHTML = `
                <div class="game-card winner" style="border-left-color: ${selectedGame.color}">
                    <div class="game-icon">🎮</div>
                    <div class="game-name" style="color: ${selectedGame.color}">${selectedGame.name}</div>
                    <div class="game-percent">${selectedGame.percentage}</div>
                </div>
            `;
            
            // 高亮显示选中的游戏
            const items = document.querySelectorAll('.game-item');
            items.forEach(item => {
                item.classList.remove('highlight');
                if (parseInt(item.dataset.index) === games.findIndex(g => g.name === selectedGame.name)) {
                    item.classList.add('highlight');
                    // 保持页面不滚动
                    const rect = item.getBoundingClientRect();
                    if (rect.top < 0 || rect.bottom > window.innerHeight) {
                        item.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
                    }
                }
            });
            
            // 更新统计数据
            if (statsPanel.style.display === 'block') {
                updateStats();
            }
        }
        
        // 更新统计数据
        function updateStats() {
            calculatePercentages();
            statsBody.innerHTML = '';
            games.forEach((game, index) => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td style="color: ${game.color}; font-weight: bold;">${game.name}</td>
                    <td>${game.weight}</td>
                    <td>${game.percentage}</td>
                `;
                statsBody.appendChild(row);
            });
        }
        
        // 更新历史记录
        function updateHistory() {
            historyList.innerHTML = '';
            history.forEach((item, index) => {
                const historyItem = document.createElement('div');
                historyItem.className = 'history-item';
                historyItem.innerHTML = `
                    <span class="history-index">${index + 1}</span>
                    <span class="history-name" style="color: ${item.color}">${item.name}</span>
                    <span class="history-time">${item.time}</span>
                `;
                historyList.appendChild(historyItem);
            });
        }
        
        // 更新编辑控件
        function updateEditControls() {
            calculatePercentages();
            editControls.innerHTML = '';
            games.forEach((game, index) => {
                const control = document.createElement('div');
                control.className = 'edit-control';
                control.innerHTML = `
                    <div class="edit-control-row">
                        <label style="color: ${game.color}">名称:</label>
                        <input type="text" class="name-input" value="${game.name}" data-index="${index}" data-type="name">
                    </div>
                    <div class="edit-control-row">
                        <label>颜色:</label>
                        <div class="input-group">
                            <input type="color" class="color-input" value="${game.color}" data-index="${index}" data-type="color">
                            <label>权重:</label>
                            <input type="number" min="0" value="${game.weight}" data-index="${index}" data-type="weight">
                        </div>
                    </div>
                    <div class="edit-control-row">
                        <span style="color: var(--primary-color);">百分比: ${game.percentage}</span>
                    </div>
                    <div class="delete-row">
                        <button class="btn-danger delete-game-btn" data-index="${index}">删除游戏</button>
                    </div>
                `;
                editControls.appendChild(control);
            });
            
            // 添加删除按钮事件
            document.querySelectorAll('.delete-game-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    const index = parseInt(this.dataset.index);
                    const gameName = games[index].name;
                    if (confirm(`确定要删除游戏 "${gameName}" 吗?`)) {
                        games.splice(index, 1);
                        updateEditControls();
                        updateGameList();
                    }
                });
            });
        }
        
        // 执行抽奖
        function performDraw() {
            // 根据设置选择游戏
            const validGames = settings.excludeZeroWeight 
                ? games.filter(game => game.weight > 0)
                : games;
                
            if (validGames.length === 0) {
                alert('没有可用的游戏，请添加游戏或设置权重');
                return null;
            }
            
            const totalWeight = validGames.reduce((sum, game) => sum + game.weight, 0);
            let randomValue = Math.random() * totalWeight;
            let accumulatedWeight = 0;
            let selectedGame = null;
            
            for (const game of validGames) {
                accumulatedWeight += game.weight;
                if (randomValue <= accumulatedWeight) {
                    selectedGame = game;
                    break;
                }
            }
            
            return selectedGame;
        }
        
        // 开始抽奖
        function startDraw() {
            autoSpinBtn.disabled = true;
            
            // 添加抽奖动画效果
            autoResult.style.animation = 'pulse 0.3s infinite';
            
            // 模拟抽奖过程
            let counter = 0;
            const maxIterations = 10;
            const validGames = settings.excludeZeroWeight 
                ? games.filter(game => game.weight > 0)
                : games;
                
            const interval = setInterval(() => {
                const randomIndex = Math.floor(Math.random() * validGames.length);
                updateResult(validGames[randomIndex]);
                
                counter++;
                if (counter >= maxIterations) {
                    clearInterval(interval);
                    autoResult.style.animation = '';
                    autoSpinBtn.disabled = false;
                    
                    // 确定最终结果
                    const winner = performDraw();
                    if (winner) {
                        updateResult(winner);
                        addToHistory(winner);
                        
                        // 如果不勾选"抽奖时不修改权重值"设置，则更新权重
                        if (!settings.keepWeight) {
                            winner.weight = Math.max(0, winner.weight - 1);
                            updateGameList();
                            updateStats();
                        }
                    }
                }
            }, 150);
        }
        
        // 添加到历史记录
        function addToHistory(game) {
            const now = new Date();
            const timeString = now.toLocaleTimeString();
            
            history.push({
                name: game.name,
                color: game.color,
                time: timeString
            });
            
            updateHistory();
        }
        
        // 保存设置
        function saveSettings() {
            const inputs = editControls.querySelectorAll('input');
            inputs.forEach(input => {
                const index = parseInt(input.dataset.index);
                const type = input.dataset.type;
                
                if (type === 'name') {
                    games[index].name = input.value.trim() || games[index].name;
                } else if (type === 'color') {
                    games[index].color = input.value;
                } else if (type === 'weight') {
                    games[index].weight = parseInt(input.value) || 0;
                }
            });
            
            updateGameList();
            togglePanel('manage');
        }
        
        // 重置权重
        function resetWeights() {
            games.forEach(game => {
                game.weight = 30;
            });
            updateEditControls();
            updateGameList();
            updateStats();
        }
        
        // 添加新游戏
        function addNewGame() {
            const name = newGameName.value.trim();
            const color = newGameColor.value;
            const weight = parseInt(newGameWeight.value) || 30;
            
            if (!name) {
                alert('请输入游戏名称');
                return;
            }
            
            if (games.some(game => game.name === name)) {
                alert('该游戏名称已存在');
                return;
            }
            
            games.push({
                name: name,
                color: color,
                weight: Math.max(0, weight)
            });
            
            newGameName.value = '';
            newGameWeight.value = '30';
            
            updateGameList();
            togglePanel('add-game');
        }
        
        // 随机分配权重
        function randomizeWeights() {
            const activeGames = settings.excludeZeroWeight 
                ? games.filter(game => game.weight > 0)
                : games;
            
            if (activeGames.length === 0) return;

            const minWeight = activeGames.length;
            const totalWeight = minWeight + Math.floor(Math.random() * 100);
            
            let remainingWeight = totalWeight;
            for (let i = 0; i < activeGames.length; i++) {
                if (i === activeGames.length - 1) {
                    activeGames[i].weight = remainingWeight;
                } else {
                    const max = Math.floor(remainingWeight * 0.7);
                    activeGames[i].weight = Math.max(1, Math.floor(Math.random() * max) + 1);
                    remainingWeight -= activeGames[i].weight;
                }
            }
            
            // 打乱权重顺序
            for (let i = activeGames.length - 1; i > 0; i--) {
                const j = Math.floor(Math.random() * (i + 1));
                [activeGames[i].weight, activeGames[j].weight] = [activeGames[j].weight, activeGames[i].weight];
            }
            
            updateGameList();
            updateStats();
        }
        
        // 清空历史记录
        function clearHistory() {
            if (confirm('确定要清空中奖记录吗?')) {
                history = [];
                updateHistory();
            }
        }
        
        // 保存系统设置
        function saveSystemSettings() {
            settings.keepWeight = keepWeightSetting.checked;
            settings.excludeZeroWeight = excludeZeroSetting.checked;
            updateGameList();
            togglePanel('settings');
        }
        
        // 加载系统设置
        function loadSystemSettings() {
            keepWeightSetting.checked = settings.keepWeight;
            excludeZeroSetting.checked = settings.excludeZeroWeight;
        }
        
        // 切换面板显示
        function togglePanel(type) {
            if (type === 'manage') {
                managePanel.style.display = managePanel.style.display === 'none' ? 'block' : 'none';
                if (managePanel.style.display === 'block') {
                    updateEditControls();
                    addGamePanel.style.display = 'none';
                    statsPanel.style.display = 'none';
                    historyPanel.style.display = 'none';
                    settingsPanel.style.display = 'none';
                }
            } else if (type === 'add-game') {
                addGamePanel.style.display = addGamePanel.style.display === 'none' ? 'block' : 'none';
                if (addGamePanel.style.display === 'block') {
                    managePanel.style.display = 'none';
                    statsPanel.style.display = 'none';
                    historyPanel.style.display = 'none';
                    settingsPanel.style.display = 'none';
                }
            } else if (type === 'stats') {
                statsPanel.style.display = statsPanel.style.display === 'none' ? 'block' : 'none';
                if (statsPanel.style.display === 'block') {
                    updateStats();
                    managePanel.style.display = 'none';
                    addGamePanel.style.display = 'none';
                    historyPanel.style.display = 'none';
                    settingsPanel.style.display = 'none';
                }
            } else if (type === 'history') {
                historyPanel.style.display = historyPanel.style.display === 'none' ? 'block' : 'none';
                if (historyPanel.style.display === 'block') {
                    managePanel.style.display = 'none';
                    addGamePanel.style.display = 'none';
                    statsPanel.style.display = 'none';
                    settingsPanel.style.display = 'none';
                }
            } else if (type === 'settings') {
                settingsPanel.style.display = settingsPanel.style.display === 'none' ? 'block' : 'none';
                if (settingsPanel.style.display === 'block') {
                    loadSystemSettings();
                    managePanel.style.display = 'none';
                    addGamePanel.style.display = 'none';
                    statsPanel.style.display = 'none';
                    historyPanel.style.display = 'none';
                }
            }
        }
        
        // 事件监听
        autoSpinBtn.addEventListener('click', startDraw);
        saveBtn.addEventListener('click', saveSettings);
        resetBtn.addEventListener('click', resetWeights);
        confirmAddBtn.addEventListener('click', addNewGame);
        randomBtn.addEventListener('click', randomizeWeights);
        clearHistoryBtn.addEventListener('click', clearHistory);
        saveSettingsBtn.addEventListener('click', saveSystemSettings);
        
        // 面板按钮事件
        manageBtn.addEventListener('click', () => togglePanel('manage'));
        addGameBtn.addEventListener('click', () => togglePanel('add-game'));
        statsBtn.addEventListener('click', () => togglePanel('stats'));
        historyBtn.addEventListener('click', () => togglePanel('history'));
        settingsBtn.addEventListener('click', () => togglePanel('settings'));
        
        closeManagePanel.addEventListener('click', () => togglePanel('manage'));
        closeAddGamePanel.addEventListener('click', () => togglePanel('add-game'));
        closeStatsPanel.addEventListener('click', () => togglePanel('stats'));
        closeHistoryPanel.addEventListener('click', () => togglePanel('history'));
        closeSettingsPanel.addEventListener('click', () => togglePanel('settings'));
        
        // 初始化
        calculatePercentages();
        updateGameList();
        updateHistory();
    </script>
</body>
</html>